package cn.uexpo.lhz.controller.admin;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.uexpo.lhz.biz.HallBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.common.oldUtils.StringDate;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.Expo;
import cn.uexpo.lhz.domain.Hall;
import cn.uexpo.lhz.domain.HallExample;


@RestController
@RequestMapping("/admin/hall")
public class HallController extends BaseController<Hall>{

	@Autowired
	private HallBiz hallBiz;



		/**
		 * 列表
		 * @return
		 * @throws Exception
		 */

//		@PostMapping(value="/findPage")
//		@RequestMapping(value = "/findPage", method = { RequestMethod.GET, RequestMethod.POST })
//		public HttpResult findPage(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
//			PageResult result=findPage1(pageRequest);
//			return HttpResult.ok(result);
//		}


//		private PageResult findPage1(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
//
//			Object hallName = pageRequest.getParamValue("hallName");//展会名称
//			HallExample example=new HallExample();
//			String orderBy=" HALL.createTime ";//按照序号降序
//		    String sort=" desc ";//默认按照时间降序
//			example.setOrderByClause(orderBy+sort);
//			HallExample.Criteria criteria=example.createCriteria();
//			if (StringUtils.isNotBlank((String)hallName)) {
//				criteria.andNameLike("%"+(String)hallName+"%");
//			}
//			PageResult result=hallBiz.findPage(pageRequest,example);
//			return result;
//		}


		/**
	     * 添加/修改
	     * @param record
	     * @return
		 * @throws Exception
	     */
		@PostMapping(value="/save")
		public HttpResult save(@RequestBody Hall record) {
	    	String msg="";
	    	long result=0;
	    	if (record.getId()==null) {
//	    		名称不能重复
	    		HallExample example=new HallExample();
	    		HallExample.Criteria citeria=example.createCriteria();
	    		citeria.andNameEqualTo(record.getName());
	    		List<Hall> records=hallBiz.selectByExample(example);
	    		if(records!=null&&records.size()>0) {
	    			msg="展馆名已存在！";
	    		}else {
	    			record.setUsable(1);//可用
//	    			record.setIsshow(0);//不展示
	    			record.setCreatetime(StringDate.getNow());
	    			result=hallBiz.insertSelective(record);
	        	    if(result==1) {
	            		msg="添加展馆成功！";
	            	}else {
	            		msg="添加展馆失败！";
	        		}
	    		}
			}else {
				result=hallBiz.updateByPrimaryKeySelective(record);
				if(result==1) {
		    		msg="修改展馆成功！";
		    	}else {
		    		msg="修改展馆失败！";
				}
			}

	    	if(result==1) {
				return HttpResult.ok(msg);
			}else {
				return HttpResult.error(msg);
			}
		}

	    /**
	     * 删除
	     * @param
	     * @return
	     * @throws Exception
	     */

	    @RequestMapping(value="/delete",method=RequestMethod.POST)
	    public HttpResult dictDelete(@RequestBody List<Hall> records) throws Exception {
			int result=0;
			try {
				result= hallBiz.delete(records);
			} catch (Exception e) {
				result=0;
			}

			return HttpResult.ok(result);

		}


		/**
		 *
		 * @Title: updateUsableStatus
		 * @Description: TODO(更新可用状态)
		 * @param: @param record
		 * @param: @return
		 * @param: @throws Exception
		 * @return: HttpResult
		 * @throws
		 */
		@RequestMapping(value="/updateUsableStatus",method=RequestMethod.POST)
		public HttpResult updateUsableStatus(@RequestBody Object obj) throws Exception {
			Map map=(Map)obj;
			int id=(Integer)map.get("id");
			int usable=(Integer)map.get("usable");
			System.out.println(id+"-----------"+usable);
			int result=0;
			String msg="状态更新失败!";
			Hall record = hallBiz.selectByPrimaryKey(id);
			record.setUsable(usable);
			result=hallBiz.updateByPrimaryKeySelective(record);
			if(result==1) {
				 msg="状态更新成功!";
				return HttpResult.ok(msg);
			}else {
				return HttpResult.error(msg);
			}
		}




}
